import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_module/common/base/base_page.dart';
import 'package:flutter_module/common/extensions/cached_network_image_extension.dart';
import 'package:flutter_module/model/cooperation_detail_mo.dart';
import 'package:flutter_module/style/colors.dart';

/// Create by kyluzoi
/// Date: 2021-08-25
///
/// Description:

class CaseDetailsPage extends BaseStateFulWidget {
  final Map params;

  const CaseDetailsPage({Key? key, required this.params}) : super(key: key);

  @override
  BaseState<StatefulWidget> buildState() {
    return _CaseDetailsPageState();
  }
}

class _CaseDetailsPageState extends BaseState<CaseDetailsPage> with BasePage {
  late CaseListMo _caseListMo;

  @override
  String get appBarTitle => _caseListMo.title ?? "案例详情";

  @override
  void initState() {
    _caseListMo = CaseListMo.fromJson(widget.params);
    super.initState();
  }

  @override
  Widget createView(BuildContext context) {
    List<String> images = _caseListMo.images?.split(",") ?? [];

    return Container(
        color: Colors.white,
        height: double.infinity,
        child: ListView.builder(
          itemBuilder: (context, index) {
            if (index == 0)
              return _caseHeader;
            else
              return _renderItem(images[index - 1], index - 1);
          },
          itemCount: images.length + 1,
        ));
  }

  get _caseHeader => Column(
        children: [
          SizedBox(
            height: 20,
          ),
          Text(
            "— 案例描述 —",
            textAlign: TextAlign.center,
            style: TextStyle(color: ThemeColors.text333, fontSize: 14),
          ),
          SizedBox(
            height: 11,
          ),
          Padding(
            padding: EdgeInsets.only(left: 16, right: 16),
            child: Text(
              _caseListMo.content ?? "",
              style: TextStyle(color: ThemeColors.text888, fontSize: 12),
            ),
          ),
          SizedBox(
            height: 22,
          ),
          Text("— 案例详情 —",
              textAlign: TextAlign.center,
              style: TextStyle(color: ThemeColors.text333, fontSize: 14)),
          SizedBox(
            height: 11,
          ),
        ],
      );

  Widget _renderItem(String image, index) {
    var imageWidget = YFNetworkImage(
      resource: image,
    );
    return index == 0
        ? Hero(
            tag: "photo-$image",
            child: imageWidget,
          )
        : imageWidget;
  }
}
